package com.shou.domain.park.service.impl;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.shou.domain.common.AbstractService;
import com.shou.domain.common.PageQueryEntity;
import com.shou.domain.common.ParkingSpaceInfoEntity;
import com.shou.domain.park.model.ParkingSpaceEntity;
import com.shou.domain.park.repository.IParkingSpaceRepository;
import com.shou.domain.park.service.IParkingSpaceService;
import com.shou.types.common.ReturnCodeEnum;
import com.shou.types.common.TableNameEnum;
import com.shou.types.exception.DataNotFoundException;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author Nemo
* @description 针对表【parkingspaces(车位表，记录每个车位的状态和相关信息)】的数据库操作Service实现
* @createDate 2025-02-07 22:10:51
*/
@Service
public class ParkingSpaceService extends AbstractService implements IParkingSpaceService {
    @Resource
    private IParkingSpaceRepository parkingSpaceRepository;
    @Override
    public void insertParkingSpace(ParkingSpaceEntity parkingSpace) {
        validateParkingSpaceEntity(parkingSpace);
        parkingSpaceRepository.insert(parkingSpace);
    }

    @Override
    public ParkingSpaceEntity getParkingSpaceById(Integer id) {
        return parkingSpaceRepository.getParkingSpaceById(id);
    }

    @Override
    public IPage<ParkingSpaceEntity> pageQuery(PageQueryEntity pageQueryEntity) {
        pageQueryEntity.setSortName(camelToSnake(pageQueryEntity.getSortName()));
        validateQueryParams(pageQueryEntity, TableNameEnum.PARKING_SPACE);
        return parkingSpaceRepository.getParkingSpacesPage(pageQueryEntity);
    }

    @Override
    public void updateParkingSpaceById(ParkingSpaceEntity parkingSpaceEntity) {
        Integer id = parkingSpaceEntity.getId();
        if(id == null || !checkIfExistById(id, TableNameEnum.PARKING_SPACE)){
            throw new DataNotFoundException(DataNotFoundException.ErrorCodeEnum.SPACE_INFO_NOT_FOUND, "id: " + id, ReturnCodeEnum.RC410);
        }
        validateParkingSpaceEntity(parkingSpaceEntity, parkingSpaceEntity.getId());
        parkingSpaceRepository.updateParkingSpaceById(parkingSpaceEntity);
    }

    @Override
    public ParkingSpaceInfoEntity getSpacesAvailableInfoByCode(String lotCode) {
        if(!checkIfExistByCode(lotCode, TableNameEnum.PARKING_LOT.getEnTableName())){
            throw new DataNotFoundException(DataNotFoundException.ErrorCodeEnum.LOT_INFO_NOT_FOUND, "编号: " + lotCode, ReturnCodeEnum.RC410);
        }
        return parkingSpaceRepository.getSpacesAvailableInfoByCode(lotCode);
    }

    @Override
    public ParkingSpaceEntity getParkingSpaceByCode(String lotCode, String spaceCode) {
        if(!checkIfExistByCode(lotCode, TableNameEnum.PARKING_LOT.getEnTableName())){
            throw new DataNotFoundException(DataNotFoundException.ErrorCodeEnum.LOT_INFO_NOT_FOUND, "编号: " + lotCode, ReturnCodeEnum.RC410);
        }
        return parkingSpaceRepository.getParkingSpaceByCode(lotCode, spaceCode);
    }

    @Override
    public void deleteByIds(List<Integer> ids) {
        super.deleteByIds(ids, TableNameEnum.PARKING_SPACE);
    }

    @Override
    public void deleteAll() {
        super.deleteAll(TableNameEnum.PARKING_SPACE);
    }

    private void validateParkingSpaceEntity(ParkingSpaceEntity parkingSpace, Integer... excludeIds) {
        validateByLotCode(parkingSpace.getCode(), TableNameEnum.PARKING_SPACE.getEnTableName(), parkingSpace.getLotCode(), excludeIds);
    }
}




